#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/3/28 20:43
# @Author  : Seven
# @File    : GuiServer.py
# @Software: PyCharm
# function : QT实现界面
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from visualization import main
import os
rootdir = os.getcwd()


class filedialogdemo(QWidget):
    def __init__(self, parent=None):
        super(filedialogdemo, self).__init__(parent)
        self.image_path = ''
        self.function()
        self.initUI()

    def function(self):
        layout = QVBoxLayout()
        self.label = QLabel()
        layout.addWidget(self.label)
        self.btn = QPushButton()
        self.btn.clicked.connect(self.loadFile)
        self.btn.setText("选择车辆图片")
        self.btn.setFont(QFont('Microsoft YaHei', 18))
        self.btn.setStyleSheet(
            "QPushButton{color:rgb(101,153,26)}"  # 按键前景色
            "QPushButton{background-color:rgb(198,224,205)}"  # 按键背景色
            "QPushButton:hover{color:red}"  # 光标移动到上面后的前景色
            "QPushButton{border-radius:6px}"  # 圆角半径
            "QPushButton:pressed{background-color:rgb(180,180,180);border: None;}"  # 按下时的样式
        )

        self.btn_2 = QPushButton()
        self.btn_2.clicked.connect(self.train)
        self.btn_2.setText("检测&&识别")
        self.btn_2.setFont(QFont('Microsoft YaHei', 18))
        self.btn_2.setStyleSheet(
            "QPushButton{color:rgb(101,153,26)}"  # 按键前景色
            "QPushButton{background-color:rgb(198,224,205)}"  # 按键背景色
            "QPushButton:hover{color:red}"  # 光标移动到上面后的前景色
            "QPushButton{border-radius:6px}"  # 圆角半径
            "QPushButton:pressed{background-color:rgb(180,180,180);border: None;}"  # 按下时的样式
        )

        layout.addWidget(self.btn)
        layout.addWidget(self.btn_2)
        self.setLayout(layout)

    def initUI(self):
        # 背景图设置
        self.label.setPixmap(QPixmap(os.path.join(rootdir, 'data/background.jpg')).scaled(900, 800))
        self.resize(900, 800)  # 窗口尺寸
        self.center()  # 窗口居中
        self.setWindowTitle('车辆检测与识别--Power By Seven')
        self.setWindowIcon(QIcon(os.path.join(rootdir, 'data/logo.jpg')))

    def center(self):
        screen = QDesktopWidget().screenGeometry()
        size = self.geometry()
        self.move((screen.width() - size.width()) / 2,
                  (screen.height() - size.height()) / 2 - 40)

    def loadFile(self):
        print("读取图片")
        self.image_path, status = QFileDialog.getOpenFileName(self, '选择图片', 'c:\\', 'Image files(*.jpg *.gif *.png)')
        if self.image_path:
            self.label.setPixmap(QPixmap(self.image_path).scaled(900, 800))
        else:
            self.label.setPixmap(QPixmap(os.path.join(rootdir, 'data/background.jpg')).scaled(900, 800))

    def train(self):
        if self.image_path:
            print("开始检测")
            status, car_scores, class_scores = main(self.image_path)
            print("检测完成")
            if status:
                self.label.setPixmap(QPixmap(os.path.join(rootdir, 'data/nocar.jpg')).scaled(900, 800))
            else:
                self.label.setPixmap(QPixmap(os.path.join(rootdir, 'result.jpg')).scaled(900, 800))
                id = 1
                msg = ''
                for s, c in zip(car_scores, class_scores):
                    text = "{}:检测结果的准确率：{:.2f}, 分类结果的准确率：{:.2f}".format(id, s, c)
                    msg += text + '\n'
                    id += 1
                QMessageBox.information(self, "检测结果", self.tr(msg))
            self.image_path = 0
        else:
            QMessageBox.warning(self, "警告", "请先选择图片", QMessageBox.Cancel)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    fileload = filedialogdemo()
    fileload.show()
    sys.exit(app.exec_())
